use axum::Json;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

#[derive(Serialize,Deserialize,Clone)]
pub enum MyResponse{
    Success(Value),
    Error(Value),
    Unauthorized(Value),
    Forbidden(Value),
    NotFound(Value),
    UnprocessableEntity(Value),
    SqlxError(Value),
    OtheError(Value)
}

// //实现序列化和反序列化以及clone的
#[derive(Serialize)]
pub struct  MyResponseStruct{
    code:i32,
    message:String,
    result:Value
}


impl  MyResponse {
    pub fn into(self) -> Json<MyResponseStruct> {
        match self {
            MyResponse::Success(value) =>(MyResponseStruct {  code:200, message:String::from("success"), result: json!(value) }).into(),
            MyResponse::Error(value) =>(MyResponseStruct {  code:202, message:String::from("error"), result: json!(value) }).into(),
            MyResponse::Unauthorized(value) => MyResponseStruct {  code:201, message:String::from("unauthorized"), result: json!(value) }.into(),
            MyResponse::Forbidden(value) => MyResponseStruct {  code:403, message:String::from("forbidden"), result: json!(value) }.into(),
            MyResponse::NotFound(value) => MyResponseStruct {  code:404, message:String::from("notfound"), result: json!(value) }.into(),
            MyResponse::UnprocessableEntity(value) => MyResponseStruct {  code:200, message:String::from("unprocessable_entity"), result: json!(value) }.into(),
            MyResponse::SqlxError(value) => MyResponseStruct {  code:404, message:String::from("SqlxError"), result: json!(value) }.into(),
            MyResponse::OtheError(value) => MyResponseStruct {  code:500, message:String::from("OtheError"), result: json!(value) }.into(),
        }
    }
}
